﻿using RM.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RM.Services
{
    public class RecordTimeService : IRecordTimeService
    {
        private readonly IRespository<RecordDay> _recordDayRespository;
        private readonly IRespository<RecordTime> _recordTimeRespository;

        public RecordTimeService(IRespository<RecordDay> recordDayRespository , IRespository<RecordTime> recordTimeRespository)
        {
            this._recordDayRespository =  recordDayRespository;//new EfRespository<RecordDay>();//
            this._recordTimeRespository = recordTimeRespository;//new EfRespository<RecordTime>();//
        }

        public int InsertRecordDay(RecordDay recordDay)
        {
            if (recordDay == null)
                throw new ArgumentException("recordDay");

            return this._recordDayRespository.Insert(recordDay) ;
        }

        public bool InsertRecordTime(RecordTime recordTime)
        {
            if (recordTime == null)
                throw new ArgumentException("recordTime");
            return this._recordTimeRespository.Insert(recordTime) > 0;
        }

        public RecordDay GetByDateTime(DateTime dateTime)
        {
            if (dateTime == null)
                throw new ArgumentException("dateTime");
            var query = this._recordDayRespository.Get(p => p.Date != null && p.Date.Value.Year == dateTime.Year && p.Date.Value.Month == dateTime.Month && p.Date.Value.Day == dateTime.Day);
            return query.FirstOrDefault();
        }

        public bool UpdateRecordDay(RecordDay recordDay)
        {
            if (recordDay == null)
                throw new ArgumentException("recordDay");
            this._recordDayRespository.Update(recordDay);
            return true;
        }

        public bool UpdateRecordTime(RecordTime recordTime)
        {
            if (recordTime == null)
                throw new ArgumentException("recordTime");
            this._recordTimeRespository.Update(recordTime);
            return true;
        }

        public bool DeleteRecordDay(RecordDay recordDay)
        {
            if (recordDay == null)
                throw new ArgumentException("recordDay");
            foreach (RecordTime rt in recordDay.RecordTimeList)
            {
                this._recordTimeRespository.Delete(rt);
            }
            this._recordDayRespository.Delete(recordDay);

            return true;
        }

        public bool DeleteRecordTime(RecordTime recordTime)
        {
            if (recordTime == null)
                throw new ArgumentException("recordTime");
            return this._recordTimeRespository.Delete(recordTime)>0;
        }
    }
}
